package com.lidsu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lidsu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119682681";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCav19FhG07nxgRdY1BplPNSOIH3LQgOAoYxjuQDXTGuiGAVbc1DWt16ecx56JaXQSuEWaC70MB93D2mSFVldo0OqI66qjoqh/LMEWBpdQ/PB9FgTpLUv5G4hZ2q0E+wvy8++kyUtmGUlYfmiqolv+MEPcA/xXfh9OYINMGvruVNeRx0JSefNEZ+M/I9Svi+a+r/vACqwZsenIlp9m4Rd6yjvQKSVky+2kq/qUHhyTctcOPFJgmLA+WkbW8+fAanIcupzPahgxAS3KQqiUDVKc+jU99DBNQtaA15+MAucpF2FQ4HnHwKsL96O2qBwm+II/PJ5/fWoDD2RuUWUm64AWFAgMBAAECggEAXB0cdgIND5d1OTdTHUgHzEPdIDb1FdSl1fdyDx6ja4PyWzUquC7Dz82q7F7omoeSOJdtAPWxWmNTX8bpO8vWto5McWurncte35FdXn46qGV+a94vuBSv/bupCXyJevWQgmIlfuooR7eNQoR/bhlsBpE2puYwu/1bK8wmMXaprHgVyXb9/Zb2WdyfJweO2R/m9KTx01I93GgHRGM/6PS3Xb0uUdvJALwHcAqz4nPY7RBXbGTj6s/SmDT0hqpOFceQWJjfq0jCePiFHGMH0sPqcxoS34qsm6uecUqN9wyJc4XPe5HMTozcJ2YEawL0Jb3okEzVtdwmKAMbnv4mATUu4QKBgQDwkHgrEmHKHeJhLHQtLfcK1yVRyLjGxz1/C5NjEfJAAmxjyimgJ/+69OvCmauI85dVwJI3uVoWn7wGNY6MtkCdxjei5gafqVkJp1kpC3xXKqPyuRjupRwb3J6DKq90/UDxWtOxjzN4NuECYQLQCV+xaip105xFlYpdBIhf5y71uQKBgQCkrUTOz+g1qBdKqLfX0j2AC+NplQyxe6/HLZEBqcEsr4O6+HGZ17E+ZzKnIAL5NpqrOXp+TGQp58BEqkrar8gCyijPbug88WGgVjdKqfxOKwU4IzDSJCPjTHAQT46Ne3d+XURmaHPRv4d21n8HxPLboxCxhN0N0KLjUqac9f90LQKBgQCvX5LLS6ZQs4KSdtbnDOhZVckWGy9hcmSJlsXG5pFlSp78DKhV4nMTN3QcNO0RdQuxbybpSs27So3HNQoL2N24Z4Wt0uWnRczsNbzRKFdoMGS/EWN0rpeUVDyWwOqSbgIHMcC8Y/oQvGlC07g6M2s4qn3z0eXN8Jui/ifxSlc4uQKBgHap6LBCfujTRF1nMHrTo792UZwObIQkRaj4O8qAAZRqsTj0uB+MnssB4C0v0xSz9UY29/60m2Ta++A++guVqUHhXQEEwVLQ+XFpTxR9YzMMqwklypNSuY2b0hDe0zE+a//hw10sHXFiW4IV1JnLoNkGKwxQWl4PHqFCOI9YcIjNAoGAKOTTbWPKLpAfcF9wWDaX6vOkuoR3qGGx1mYrtjSi+3M2o9MgYYATLSOl/LaLzxTw2/yPDHog++1aXfY8F9gLr/CNnbPz+FHm8q3YSBdcdREL2C09GoxP3U9FN/dNJbwaKZPuzBLVTRjJCaOju9gMIULi0Dzc4i0kS1LHwoagUmw=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApSed2+pQTnAaEYMVbiM9Psu+Sq2OzJtOH1c3r0NFvMrP/wSw+3D7+uDVqD9Eb1XbcZj4QcOH6C8ROAfhkl3lByjkuGlVSfRw6ymla0bWNuKWCIWyxh+Q7xXtZbrWVGG2+6RcSrdtDxSUcwl+1f44mXUTBT+6XxKQyeXUC2HTQjwUe0fx/8KtRrn5D3p+A7lw0XlzUEOkS/NpvjXk7AptLaIVVaL6mM43aogpfNhvcRMXKkE8hIqCfVMI0AqvDiThEdfftM+laVlD5Ib/ILvVS3dwB52eFWtRqxXhGHPWbo1rZWWsZNM9DA9qVWvnK1KbsNPon6pXymz2YALyk/Z5tQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://5s2u632061.51vip.biz/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.lidsu.icu/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
